package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrModuleVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.CommonAttrModuleHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BigDataOrderModleHandler extends CommonAttrModuleHandler {
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private IOperateSceneInstService operateSceneInstService;
    @Override
    public CommonAttrModuleVO dealExecuteSpecialAttr(AcceptContext context, SceneModule sceneModule, CommonAttrModuleVO defCommonAttrInfoVO) {
        this.removeCommonAttr(context, sceneModule, defCommonAttrInfoVO);
        return defCommonAttrInfoVO;
    }

    @Override
    public CommonAttrModuleVO dealPreviewSpecialAttr(AcceptContext context, SceneModule sceneModule, CommonAttrModuleVO defCommonAttrInfoVO) {
        this.removeCommonAttr(context, sceneModule, defCommonAttrInfoVO);
        return defCommonAttrInfoVO;
    }
    private CommonAttrModuleVO removeCommonAttr(AcceptContext context, SceneModule sceneModule, CommonAttrModuleVO defCommonAttrInfoVO) {
        String sceneInstId = context.getSceneVo().getSceneInstId();
        String rootInst = querySceneInstService.getSceneInst(sceneInstId).getRootInstId();
        KiteOfferInst offerInst = querySceneInstService.getOfferInst(context.getSceneVo().getCustId(), rootInst);
        String offerId = KiteStringUtils.isEmpty(offerInst.getOfferId())?offerInst.getSpecId():offerInst.getOfferId();
        String objId = OfferCache.get(offerId).getAttrValue("cateId");
        List<DcPublic> dcPublics = DcPublicCache.getByPcode("20210727702", "BigDataOrderModleHandler", objId);
        if(KiteListUtils.isNotEmpty(dcPublics)){
            if(!dcPublics.stream().anyMatch(dp->KiteStringUtils.equals(dp.getCodea(),offerId))){
                defCommonAttrInfoVO.setIsShow("F");
                defCommonAttrInfoVO.getCommonAttrList().forEach(cmSttr->{
                    KiteAttr rmAttr = querySceneInstService.getCommonAttr(sceneInstId, cmSttr.getFieldName());
                    rmAttr.setShowType("E");
                    operateSceneInstService.setCommonAttr(context.getSceneVo().getSceneInstId(),rmAttr);
                });
                return null;
            }
        }
        return defCommonAttrInfoVO;
    }
}
